<!-- 查询可访问资源 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row :gutter="48">
                    <a-col :md="8" :sm="24">
                        <a-form-item label="资源名字">
                            <a-input v-model="description" allow-clear placeholder="请输入资源名字" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="资源组名字">
                            <a-select v-model="groupId" allowClear placeholder="请选择" @change="getData()">
                                <a-select-option v-for="(item,i) in roleList" :value="item.id" :key="i">{{item.groupName}}</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="前端key">
                            <a-input v-model="resourceTag" allow-clear placeholder="请输入前端key" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item label="接口url">
                            <a-input v-model="url" allow-clear placeholder="请输入接口url" />
                        </a-form-item>
                    </a-col>
                    <a-col :md="8" :sm="24">
                    </a-col>
                    <a-col :md="8" :sm="24">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" @click="getData()">查询</a-button>
                            <a-button icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" @change="handleTableChange" :columns="columns" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" :data-source="dataList" :scroll="{ x: 1000, }" size="small">
            <div slot="groupId" slot-scope="groupId">
                <span>{{roleList.filter((item) => item.id == groupId).length > 0 ? roleList.filter((item) => item.id == groupId)[0].groupName : ''}}</span>
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前数据?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link">删除</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <a-modal
            title="添加、修改可访问资源信息"
            :visible="visible"
            @ok="handleOk"
            :confirm-loading="confirmLoading"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="资源组">
                        <a-select v-model="item.groupId" placeholder="请选择" allowClear>
                            <a-select-option v-for="(item,i) in roleList" :value="item.id" :key="i">{{item.groupName}}</a-select-option>
                        </a-select>
                    </a-form-item>
                    <a-form-item label="资源名称">
                        <a-input v-model="item.description" placeholder="请输入资源名称" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="前端权限key">
                        <a-input v-model="item.resourceTag" placeholder="请输入前端权限key" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="资源url">
                        <a-input v-model="item.url" placeholder="请输入资源url" allow-clear></a-input>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { columns } from './table';
import { queryResource , updateResource , queryResourceGroup } from '@/api/power';
export default {
    data () {
        return {
            description: '',
            resourceTag: '',
            groupId: undefined,
            url: '',
            isload: false,
            page: 1,
            size: 10,
            total: 0,
            dataList: [],
            roleList: [],
            item: {
                id: '',
                groupId: '', // 资源组ID
                description: '', // 资源名称
                resourceTag: '', // 前端权限key
                url: '', // 资源url
            },
            confirmLoading: false,
            visible: false,
            columns,
        };
    },
    created(){
        this.getRoleList()
        this.getData()
    },
    methods: {
        // 获取资源组的信息
        getRoleList(){
            let _this = this
            const params = {
                'groupName':null, // 资源组名称
                'page':0,
                'size':99,
            }
            queryResourceGroup(params)
            .then(res=>{
                if(res.success){
                    _this.roleList = res.data.content
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 获取所有资源信息
        getData(){
            let _this = this
            _this.isload = true
            const params = {
                'groupId':_this.groupId, // 资源组ID
                'description':_this.description, // 资源名称
                'resourceTag':_this.resourceTag, // 前端权限key
                'url':_this.url, // 接口url
                'page':_this.page - 1,
                'size':_this.size,
            }
            queryResource(params)
            .then(res=>{
                _this.isload = false
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            }).catch(err=>{
                _this.isload = false
            })
        },
        // 分页
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getData()
        },
        // 添加
        add(){
            this.item.id = ''
            this.item.groupId = undefined
            this.item.description = ''
            this.item.resourceTag = ''
            this.item.url = ''
            this.visible = true
        },
        // 修改
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        // 确认添加、修改
        handleOk(){
            let _this = this
            if(!_this.item.groupId){
                _this.$message.warning('请选择当前资源所属的资源组！')
                return
            }
            if(_this.item.description == ''){
                _this.$message.warning('请输入当前资源的名称！')
                return
            }
            if(_this.item.url == ''){
                _this.$message.warning('请输入当前资源的url！')
                return
            }
            _this.confirmLoading = true
            const parmas = {
                'id':_this.item.id,
                'url':_this.item.url,
                'groupId':_this.item.groupId,
                'description':_this.item.description,
                'resourceTag':_this.item.resourceTag,
            }
            updateResource(parmas)
            .then(res=>{
                _this.confirmLoading = false
                if(res.success){
                    _this.visible = false
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除
        del(item){
            let _this = this
            const params = {
                'id':item.id,
                'url':item.url,
                'groupId':item.groupId,
                'url':item.url,
                'isDelete':1
            }
            updateResource(params)
            .then(res=>{
                if(res.success){
                    _this.getData()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>